//
//  RegistViewController.m
//  晓礼
//
//  Created by qianfeng on 15/11/10.
//  Copyright © 2015年 qianfeng. All rights reserved.
//

#import "RegistViewController.h"
#import "Interface.h"
#import <AFNetworking/AFNetworking.h>
@interface RegistViewController ()<UIAlertViewDelegate>
{
    NSInteger _index;
    NSString *_verity;
}
@property (weak, nonatomic) IBOutlet UITextField *phoneLabel;
@property (weak, nonatomic) IBOutlet UITextField *nameLabel;
@property (weak, nonatomic) IBOutlet UITextField *pwsLabel;
@property (weak, nonatomic) IBOutlet UITextField *makesureLabel;
@property (weak, nonatomic) IBOutlet UITextField *verifyLabel;
@property (weak, nonatomic) IBOutlet UIButton *getVerityBtn;

@property(nonatomic)NSTimer *timer;
@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(180, 0, 100, 40)];
    title.text=@"账号注册";
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
    
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
    _index=60;
    [_phoneLabel becomeFirstResponder];
    _getVerityBtn.layer.cornerRadius=4;
    _getVerityBtn.clipsToBounds=YES;
}
-(void)cancel
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)startTimer
{
    _getVerityBtn.backgroundColor=[UIColor lightGrayColor];
    [_getVerityBtn setTitle:[NSString stringWithFormat:@"(%ld)秒后再获取",_index] forState:UIControlStateNormal];
    _getVerityBtn.enabled=NO;
    _timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeBtn) userInfo:nil repeats:YES];
}

-(void)changeBtn
{
     _index--;
    [_getVerityBtn setTitle:[NSString stringWithFormat:@"(%ld)秒后再获取",_index] forState:UIControlStateNormal];
    if(_index==0)
    {
        _getVerityBtn.enabled=YES;
        _getVerityBtn.backgroundColor=[UIColor colorWithRed:0.743 green:0.007 blue:0.030 alpha:1.000];
        [_getVerityBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_timer invalidate];
        _index=60;
    }
   
}
/*
 #define api_regist @"http://10.0.8.18/qfxl/index.php?s=appapi&a=reg&mobile=%@&password=%@&pincode=%@&username=%@"
 */
- (IBAction)registBtn:(id)sender {
    
    if(!_phoneLabel.text)
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"手机号不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    
    NSDictionary *dic=[[NSDictionary alloc] initWithObjectsAndKeys:_phoneLabel.text,@"mobile",_pwsLabel.text,@"password",_verifyLabel.text,@"pincode",_nameLabel.text,@"username",nil];
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    [manager POST:api_regist parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        if(!responseObject)
        {
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"网络请求失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
        }else
        {
            NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            if([dic[@"data"] isEqualToString:@"注册成功"])
            {
                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"注册成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alert show];
            }
        }
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"网络请求失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    }];

}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
}
/*
 Post请求
 
 
 #define api_verificationcode @"http://10.0.8.18/qfxl/index.php?s=appapi&a=pin&mobile=%@&tips=%@&tipsid=%@"
 您正在注册晓礼网会员，如非本人操作无需理会。
 */
- (IBAction)getVerityBtn:(id)sender {
    
    if(_phoneLabel.text.length==0)
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"手机号不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        [_phoneLabel becomeFirstResponder];
        return;
    }
    NSDictionary *dic=[[NSDictionary alloc] initWithObjectsAndKeys:_phoneLabel.text,@"mobile",@"您正在注册晓礼网会员，如非本人操作无需理会。",@"tips",@"1",@"tipsid",nil];
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    [manager POST:api_verificationcode parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        if(!responseObject)
        {
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"网络请求失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
        }else
        {
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"验证码发送成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            _verity = dic[@"pincode"];
            [self startTimer];
        }
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"网络请求失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
